package com.qing.springcloud.utils;

import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author caozhipeng
 * @date 2025/7/9
 */
// 使用Redis维护全局在线状态
public class GlobalUserStatusManager {
    private static final String STATUS_PREFIX = "user_status:";
    private final RedisTemplate<String, String> redisTemplate;

    public GlobalUserStatusManager(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void online(String userId, String instanceId) {
        redisTemplate.opsForValue().set(STATUS_PREFIX + userId, instanceId);
    }

    public void offline(String userId) {
        redisTemplate.delete(STATUS_PREFIX + userId);
    }

    public String getInstanceId(String userId) {
        return redisTemplate.opsForValue().get(STATUS_PREFIX + userId);
    }
}